package com.lsh.payment.core.service.refund.impl;

import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.api.model.refund.RefundRequest;
import com.lsh.payment.core.model.payenum.PayChannel;
import com.lsh.payment.core.model.payenum.RefundStatus;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.service.refund.IRefundBuilder;
import com.lsh.payment.core.util.Idwork.RandomUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 18/7/9
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class RefundBuilder implements IRefundBuilder {


    @Override
    public PayRefund builderRefundOrder(RefundRequest refundRequest, PayChannel payChannel) {

        PayRefund targetPayRefund = new PayRefund();
        targetPayRefund.setPaymentRefundNo(RandomUtil.snowFlakeId());
        targetPayRefund.setPayPaymentNo(refundRequest.getPay_payment_no());
        targetPayRefund.setCreateAt(new Date());
        targetPayRefund.setUpdateAt(new Date());
        targetPayRefund.setAmountType("CNY");
        targetPayRefund.setRefundAmount(new BigDecimal(refundRequest.getRefund_fee()));
        targetPayRefund.setRefundTradeId(refundRequest.getRefund_trade_id());
        targetPayRefund.setIsValid(1);
        targetPayRefund.setRefundStatus(RefundStatus.REFUND_NEW.getValue());
        targetPayRefund.setCallSystem(refundRequest.getSystem());
        targetPayRefund.setRefundChannel(payChannel.getName());
        targetPayRefund.setRefundNotifyUrl(refundRequest.getNotify_url());

        targetPayRefund.setVenderId(refundRequest.getVenderId());

        return targetPayRefund;
    }

    @Override
    public JSONObject builderRefundExt(RefundRequest refundRequest) {

        JSONObject ext = new JSONObject();
        ext.put("notify_ext", StringUtils.isNotBlank(refundRequest.getNotify_ext()) ? refundRequest.getNotify_ext() : "{}");
        ext.put("refund_type", refundRequest.getRefund_type());
        ext.put("uid",refundRequest.getUid());

        return ext;
    }

}
